package cn.dfusion.blockchain.utils;

import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class TimeUtils {

	public static long getTxTimestamp() {
		return new Timestamp(System.currentTimeMillis() / 1000).getTime();
	}

	public static <T> Map<?, ?> beanToJsonmap(T t) {
		return JSON.parseObject(JSON.toJSONString(t), HashMap.class);
	}

	// beanmap
	public static <T> Map<String, String> beanTomap(T t) {

		Map<String, String> map = new HashMap<>();

		try {
			map = org.apache.commons.beanutils.BeanUtils.describe(t);
			map.remove("class");
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	public static <T> T mapToBean(TreeMap<String, Object> map, T t) {
		BeanMap.create(t).putAll(map);
		return t;
	}

}
